package moteur;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeMap;

import utils.CollectionOps;
import utils.Index;
import utils.Vector;

public class NearSearchDoubleIndex  extends NormalSearch{
	/**
	 *  tfitf Search which could treat the Search contains "+"
	 *  it uses two index: with stemming and without normalization
	 */
	//Near Search No
	private NearSearch nsStemm;
	private NearSearch nsNonStemm;
	ArrayList<String> queryStemm;
	ArrayList<String> querySansStemm;
	private Index indexSansStemm;
	private Index indexStemm;
	
	
	public NearSearchDoubleIndex(Index indexSansStemm,  Index indexStemm, ArrayList<String> searchQuery,
														ArrayList<String> nonNormalizationWords)
	{
		this.queryStemm=new ArrayList<String>(searchQuery);
		this.queryStemm.removeAll(nonNormalizationWords);
		this.querySansStemm=nonNormalizationWords;
		
		this.nsNonStemm=new NearSearch(querySansStemm, indexSansStemm);
		this.nsStemm=new NearSearch(queryStemm, indexStemm);
		this.indexSansStemm=indexSansStemm;
		this.indexStemm=indexStemm;
	}
	
	public TreeMap<Integer, HashSet<String> > getPriorityList()
	{
		HashSet<String> resultUnion=new HashSet<String>();
		
		for(String word: this.queryStemm)
		{
			if(this.indexStemm.hasEntry(word))
			{
				resultUnion=CollectionOps.union(resultUnion, this.indexStemm.get(word).keySet());
			}
		}
		for(String word: this.querySansStemm)
		{
			if(this.indexSansStemm.hasEntry(word))
			{
				resultUnion=CollectionOps.union(resultUnion, this.indexSansStemm.get(word).keySet());
			}
		}
		
		TreeMap<Integer, HashSet<String> > nbOccurrence=new TreeMap<Integer, HashSet<String> >(Collections.reverseOrder());
		for(String filename: resultUnion)
		{
			int occurs=0;
			for(String word :queryStemm)
			{
				if(this.indexStemm.hasEntry(word)&&this.indexStemm.get(word).containsKey(filename))
					occurs++;
			}
			for(String word : this.querySansStemm)
			{
				if(this.indexSansStemm.hasEntry(word)&&this.indexSansStemm.get(word).containsKey(filename))
				{
					occurs++;
				}
			}
			if(!nbOccurrence.containsKey(occurs))
			{
				nbOccurrence.put(occurs, new HashSet<String>());
			}
			nbOccurrence.get(occurs).add(filename);
				
		}
		return nbOccurrence;
	}
	
	public void rankDocs(LinkedHashSet<String> lhs,HashSet<String> filenames)
	{	
		if(filenames!=null)
		{	
			if(filenames.size()!=0)
			{
				TreeMap<Double, HashSet<String>> rankMap=new TreeMap<Double, HashSet<String>>(Collections.reverseOrder());
				for(String filename: filenames)
				{
					Vector tfVecStemm= this.nsStemm.getTf(filename);
					double tfitfStemm=tfVecStemm.multiply(this.nsStemm.getQueryVec());
					Vector tfVecSansStemm=this.nsNonStemm.getTf(filename);
					double tfitfNonStemm=tfVecSansStemm.multiply(this.nsNonStemm.getQueryVec());
					
					double tfitf=tfitfStemm+tfitfNonStemm;
					
					if(rankMap.containsKey(tfitf))
					{
						rankMap.get(tfitf).add(filename);
					}
					else
					{
						HashSet<String> hs=new HashSet<String>();
						hs.add(filename);
						rankMap.put(tfitf, hs);
					}
				}
				for(Double d : rankMap.keySet())
				{
					System.out.println(d);
					for(String filename: rankMap.get(d))
					{
						lhs.add(filename);
					}
				}
			}
		}
	}

	@Override
	public void print() {
		// TODO Auto-generated method stub
		System.out.println("NearSearchDoubleIndex: ");
		System.out.print("Sans Normalisation:	");
		System.out.println(this.querySansStemm);
		
		System.out.print("Avec Stemming: ");
		System.out.println(this.queryStemm);
		
	}
	
	

}
